package com.gxuwz.hmr.project.system.region.controller;

import java.util.List;

import com.gxuwz.hmr.common.utils.StringUtils;
import com.gxuwz.hmr.common.utils.poi.ExcelUtil;
import com.gxuwz.hmr.project.system.region.domain.Region;
import com.gxuwz.hmr.project.system.region.service.IRegionService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.region.domain.Region;
import com.ruoyi.project.system.region.service.IRegionService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.domain.Ztree;

/**
 * 行政区域信息Controller
 * 
 * @author ruoyi
 * @date 2021-05-05
 */
@Controller
@RequestMapping("/system/region")
public class RegionController extends BaseController
{
    private String prefix = "system/region";

    @Autowired
    private IRegionService regionService;

    @RequiresPermissions("system:region:view")
    @GetMapping()
    public String region()
    {
        return prefix + "/region";
    }

    /**
     * 查询行政区域信息树列表
     */
    @RequiresPermissions("system:region:list")
    @PostMapping("/list")
    @ResponseBody
    public List<Region> list(Region region)
    {
        List<Region> list = regionService.selectRegionList(region);
        return list;
    }

    /**
     * 导出行政区域信息列表
     */
    @RequiresPermissions("system:region:export")
    @Log(title = "行政区域信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Region region)
    {
        List<Region> list = regionService.selectRegionList(region);
        ExcelUtil<Region> util = new ExcelUtil<Region>(Region.class);
        return util.exportExcel(list, "行政区域信息数据");
    }

    /**
     * 新增行政区域信息
     */
    @GetMapping(value = { "/add/{regionId}", "/add/" })
    public String add(@PathVariable(value = "regionId", required = false) Long regionId, ModelMap mmap)
    {
        if (StringUtils.isNotNull(regionId))
        {
            mmap.put("region", regionService.selectRegionById(regionId));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存行政区域信息
     */
    @RequiresPermissions("system:region:add")
    @Log(title = "行政区域信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Region region)
    {
        return toAjax(regionService.insertRegion(region));
    }

    /**
     * 修改行政区域信息
     */
    @GetMapping("/edit/{regionId}")
    public String edit(@PathVariable("regionId") Long regionId, ModelMap mmap)
    {
        Region region = regionService.selectRegionById(regionId);
        mmap.put("region", region);
        return prefix + "/edit";
    }

    /**
     * 修改保存行政区域信息
     */
    @RequiresPermissions("system:region:edit")
    @Log(title = "行政区域信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Region region)
    {
        return toAjax(regionService.updateRegion(region));
    }

    /**
     * 删除
     */
    @RequiresPermissions("system:region:remove")
    @Log(title = "行政区域信息", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{regionId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("regionId") Long regionId)
    {
        return toAjax(regionService.deleteRegionById(regionId));
    }

    /**
     * 选择行政区域信息树
     */
    @GetMapping(value = { "/selectRegionTree/{regionId}", "/selectRegionTree/" })
    public String selectRegionTree(@PathVariable(value = "regionId", required = false) Long regionId, ModelMap mmap)
    {
        if (StringUtils.isNotNull(regionId))
        {
            mmap.put("region", regionService.selectRegionById(regionId));
        }
        return prefix + "/tree";
    }

    /**
     * 加载行政区域信息树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = regionService.selectRegionTree();
        return ztrees;
    }
}
